package com.centaline.androidsalesblog.widgets;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.AppCompatTextView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.PostMapListAdapter;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.interfaces.IRecycleViewListener;

import java.util.ArrayList;

/**
 * 地图找房租售列表
 * Created by guilin on 16/5/13.
 */
public class PostMapPop implements PopupWindow.OnDismissListener {

    private final View anchor;
    private PopupWindow popupWindow;
    private final PopupWindow.OnDismissListener onDismissListener;

    private AppCompatTextView atv_title, atv_price;
    private MRecyclerView mRecyclerView;

    private PostMapListAdapter postMapListAdapter;

    public PostMapPop(Context context, View anchor, ArrayList<PostDo> list,
                      DrawableRequestBuilder<String> requestBuilder,
                      PopupWindow.OnDismissListener onDismissListener,
                      IRecycleViewListener iRecycleViewListener) {
        this.anchor = anchor;
        this.onDismissListener = onDismissListener;
        View view = LayoutInflater.from(context).inflate(R.layout.layout_post_map_pop, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        atv_title = (AppCompatTextView) view.findViewById(R.id.atv_title);
        atv_price = (AppCompatTextView) view.findViewById(R.id.atv_price);
        mRecyclerView = (MRecyclerView) view.findViewById(R.id.mRecyclerView);
        mRecyclerView.setIRecycleViewListener(iRecycleViewListener);
        postMapListAdapter = new PostMapListAdapter(context, requestBuilder, list);
        mRecyclerView.setAdapter(postMapListAdapter);
        initPopWindow(view);
    }

    protected void initPopWindow(View view) {
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOnDismissListener(this);
    }

    public void show(CharSequence cestName) {
        atv_title.setText(cestName);
        show();
    }

    public void show(String postType, EsfEstateDo esfEstateDo) {
        atv_title.setText(esfEstateDo.getName());
        atv_price.setText("s".equalsIgnoreCase(postType) ?
                PriceUtil.esfSaleAvgPrice(esfEstateDo.getCestAvgPrice()) :
                PriceUtil.rentPrice(esfEstateDo.getCestAvgPriceR()));
        if ("s".equalsIgnoreCase(postType)) {
            atv_price.setVisibility(View.VISIBLE);
        }
        show();
    }

    public void stopRefresh(boolean hasMore) {
        mRecyclerView.stopRefresh(hasMore);
    }

    private void show() {
        if (popupWindow != null &&
                !popupWindow.isShowing()) {
            popupWindow.showAtLocation(anchor, Gravity.BOTTOM, 0, 0);
            mRecyclerView.startRefresh();
        }
    }

    @Override
    public void onDismiss() {
        onDismissListener.onDismiss();
    }
}
